package com.wxsoft.business.sys.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.model.easyui.Tree;
import com.wxsoft.business.sys.pojo.SysRoles;
import com.wxsoft.business.sys.pojo.SysUserRoles;
import com.wxsoft.business.sys.service.ISysRolesService;
import com.wxsoft.util.JsonUtil;

@Controller
@RequestMapping("/sysRoles")
public class SysRolesController extends BaseController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private ISysRolesService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/sysRoleslist";
	}
	
	@RequestMapping("/datagrid")
	public void list(HttpServletRequest request,PageHelper page, SysRoles sysRoles,HttpServletResponse response) throws IOException{
		
		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(sysRoles));
		List<SysRoles> sysRolesList = service.findAll(page, sysRoles);
		dg.setRows(sysRolesList);
		JsonUtil.toResponse(response, dg);

	}
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response, SysRoles sysRoles) {
		try {
			String r = service.add(sysRoles);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,HttpServletResponse response, SysRoles sysRoles) {
		try {
			String r = service.modify(sysRoles);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response, SysRoles sysRoles) {
		try {
			String r = service.delete(sysRoles);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}

	/**
	 * 根据用户查询角色信息
	 * @param session
	 * @param response
	 * @param userRoles
	 * @return
	 */
    @ResponseBody
	@RequestMapping(value = "/getRoleByUser")
	public List<Tree> getRoleByUser(HttpSession session,
			HttpServletResponse response, SysUserRoles userRoles) {
		
		List<SysRoles> roleList = service.getRoleByUser(userRoles);
		List<Tree> treeList = getTree(roleList);
		
		return treeList ;
//		String json = JSONArray.toJSONString(treeList);
//		System.out.println(json);
//		
//		JsonUtil.toResponse(response, json);
		// return menuTreeStr;
	}
	/**
	 * 获取角色
	 * @param session
	 * @param response
	 * @param user_id
	 * @return
	 */
    @ResponseBody
	@RequestMapping(value = "/getRoleBy")
	public List<Tree> getRoleBy(HttpSession session,
			HttpServletResponse response) {
		
    	SysRoles role = new SysRoles();
    	
		List<SysRoles> roleList = service.getRoleBy(role);
		List<Tree> treeList = getTree(roleList);
		
		return treeList ;
//		String json = JSONArray.toJSONString(treeList);
//		System.out.println(json);
//		
//		JsonUtil.toResponse(response, json);
		// return menuTreeStr;
	}
	/**
	 * 将菜单变成树形结构
	 * 
	 * @param menuList
	 * @return
	 */
//	public List<Tree> getTree(List<SysRoles> menuList) {
//		List<Tree> treeFList = new ArrayList<Tree>();
//
//		Tree nodeF = new Tree();
//		nodeF.setId("0");
//		nodeF.setText("顶级菜单");
////		addNode(nodeF, menuList);
//
//		treeFList.add(nodeF);
//		return treeFList;
//	}

	public List<Tree> getTree( List<SysRoles> roleList) {
		List<Tree> treeList = new ArrayList<Tree>();

		for (SysRoles role : roleList) {
			// 如果子节点父ID=父节点ID，角色不是树形结构，不进行树处理
//			if (pNode.getId().equals(role.getUnit_id())) {

				Tree node = new Tree();

				node.setId(role.getRole_code());
				// node.setPid(menu.getPcode());
				node.setText(role.getRole_name());
				treeList.add(node);
//				addNode(node, roleList);
//			}

		}
//		pNode.setChildren(treeList);
		return treeList;
	}

}
